<template>
  <div id="dsManagePage" style="margin-left: 15px">
    <!-- 面包屑导航区域 -->
    <el-breadcrumb
      separator-class="el-icon-arrow-right"
      style="padding: 12px 12px"
    >
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>执行器监控</el-breadcrumb-item>
      <el-breadcrumb-item>执行器列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card
      shadow="hover"
      style="margin-bottom: 15px; min-height: 580px; margin-right: 15px"
    >
      <el-row :gutter="20" style="margin: 12px 0">
        <el-col :span="8">
          <!-- 搜索与添加区域 -->
          <el-input
            v-model="queryInfo.keyword"
            placeholder="请输入内容搜索"
            :prefix-icon="Search"
            class="w-50 m-2"
          >
            <template #append>
              <el-button :icon="Search" @click="handleSearch" />
            </template>
          </el-input>
        </el-col>
      </el-row>
      <!-- 执行器列表区域  -->
      <el-table
        :data="executorList"
        v-loading="queryLoading"
        border
        stripe
        table-layout="auto"
      >
        <el-table-column label="序号" type="index"></el-table-column>
        <el-table-column label="IP地址" prop="host"></el-table-column>
        <el-table-column label="端口" prop="port"></el-table-column>
        <el-table-column label="任务数量" prop="taskNumber"></el-table-column>
        <el-table-column label="内存总量(GB)" prop="totalMemory"></el-table-column>
        <el-table-column label="已用内存(GB)" prop="usedMemory"></el-table-column>
        <el-table-column label="内存使用率(%)" :formatter="memoryUsageRateFormatter"></el-table-column>
        <el-table-column label="CPU使用率(%)" prop="cpuUsage"></el-table-column>
      </el-table>
      <!-- 页面区域 -->
      <el-pagination
        style="margin: 15px 0"
        :current-page="queryInfo.currentPage"
        :page-sizes="[10, 25, 50, 100]"
        :page-size="queryInfo.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        background
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage } from "element-plus";
import { Search } from "@element-plus/icons-vue";
import { to, httpPost } from "@/repository/network/";

const queryInfo = reactive({
  keyword: "", // 查询参数
  currentPage: 1, // 当前页码
  pageSize: 10, // 每页显示条数
});
const executorList = ref([]);
// 总数据条数
const total = ref(0);

// 表格是否加载中
const queryLoading = ref(true);

onMounted(() => {
  getExecutorList();
});

async function getExecutorList() {
  queryLoading.value = true;
  const { resp, error } = await to(httpPost("/admin/executor-info/paging", queryInfo));
  queryLoading.value = false;
  if (resp?.code != 200) {
    ElMessage.error("加载执行器列表时出错");
    return;
  }
  executorList.value = resp?.data?.data?.records;
  executorList.value = resp?.data?.data?.records;
  total.value = resp?.data?.data?.total;
}

function memoryUsageRateFormatter(row: any, column: any, cellValue: any, index: number) {
    return (row.usedMemory / row.totalMemory * 100).toFixed(2)
}

async function handleSearch() {
  getExecutorList();
}

// 监听 page size 改变的事件
function handleSizeChange(newSize: number) {
  queryInfo.pageSize = newSize;
  getExecutorList();
}
// 监听 页码值 改变的事件
function handleCurrentChange(newPage: number) {
  queryInfo.currentPage = newPage;
  getExecutorList();
}
</script>

<style>
</style>